<?php
/**
 * DolphinGTK - phpGtk framework
 *
 * @package    DolphinGTK
 * @author     Askarov Ruslan
 * @copyright  Askarov Ruslan (c) 2010.
 * @link       http://groups.google.com/group/dolphingtk
 * @license    http://www.opensource.org/licenses/gpl-license.php GPL
 * @version    0.1
 * @since      PHP v.: 5.2.1
 * @subpackage desktop
 * @category   library
 * @filesource
 */

final class OS
{
  private static $data;
  private static $os;



  /**
   * Метод для получения данных
   *
   * @access private
   * @return void
   */
  private static function get_data()
  {
    if( !self::$os && self::$data ) {
      self::$data = array_merge($_SERVER, $_ENV);
      self::$os = self::$os = get_os();
      define('OS', self::$os);
    }
  }

  /**
   * Метод по умолчанию. Разрешаем использование
   * методов, только как статические.
   *
   * @access public
   * @return void
   */
  public function __construct()
  {
    throw new Exception(
      'Class "Tray" used only static methods. And cannot be constructed!'
    );
  }



  
	public static function get_tmp()
	{
    self::get_data();

		if( function_exists('sys_get_temp_dir') ) {
			return sys_get_temp_dir() . '/';
		}
    
		if ( file_exists('/tmp/') && is_dir('/tmp/') ) {
			return '/tmp/';
		}

		$list = array('TMP', 'TEMP', 'TMPDIR');

		foreach( $list as $dir )
		{
			if( isset( self::$data[$item] ) )
			{
        $dir = self::$data[$dir];
        if( file_exists($dir) && is_dir($dir) )
				{
          return $env['$item'];
        }
			}
		}
	}




	public static function get_profile()
	{
    self::get_data();
    
		if( self::$os == 'windows' )
		{
			if(isset(self::$data['USERPROFILE'])) {
				return self::$data['USERPROFILE'] . '\\';
			}
			elseif(isset(self::$data['HOMEPATH']) && isset(self::$data['HOMEDRIVE'])) {
				return self::$data['HOMEPATH'] . self::$data['HOMEDRIVE'] . '\\';
			}
			elseif(isset(self::$data['USERNAME']) && file_exists('C:\Documents and Settings\\' . self::$data['USERNAME'])) {
				return 'C:\Documents and Settings\\' . $env['USERNAME'] . '\\';
			}
			else {
				return false;
			}
		}
		else
		{
			if( isset( $_ENV['HOME'] ) ) {
				return $_ENV['HOME'] . '/';
			}
      elseif ($os == 'macos' && isset($_ENV['USER']) && file_exists('/Users/' . $_ENV['USER'])) {
				return '/Users/' . $_ENV['USER'] . '/';
			}
      elseif (isset($_ENV['USER']) && file_exists('/home/' . $_ENV['USER'])) {
				return '/home/' . $_ENV['USER'] . '/';
			}
			else {
				return false;
			}
		}
	}




	public static function get_appdata()
	{
    self::get_data();

		if( self::$os == 'windows' ) {
			$path = self::get_profile() . 'Application Data/DolphinGTK/';
		}
		elseif( self::$os == 'macos' ) {
			$path = self::get_profile() . 'Library/Application Support/DolphinGTK/';
		}
		elseif (isset($_ENV['APPDATA'])) {
			$path = $_ENV['APPDATA'] . '/.DolphinGTK/';
		}
		else {
			$path = self::get_profile() . '.DolphinGTK/';
		}

		if( !file_exists($path) ) {
			mkdir($path);
		}
    
		return $path;
	}



	public static function get_documents()
	{
    self::get_data();

		if( self::$os == 'windows' )
		{
			$shell = new COM('WScript.Shell');
			$documents = $shell->SpecialFolders('MyDocuments');
			unset ($shell);
			return $documents . '/';
		}
		else
		{
			$home = self::get_profile();

			if( file_exists($home . 'Documents') )
			{
				return $home . 'Documents/';
			}
			else
			{
				return $home;
			}
		}
	}


	public static function exec($cmd)
	{
    self::get_data();

		if(self::$os == 'windows' ) {
			$shell = new COM('WScript.Shell');
			$return = $shell->Run($cmd, 0, false);
			unset ($shell);
			return (bool) $return;
		} else {
			return exec($cmd);
		}
	}



	public static function launch($file)
	{
    self::get_data();

		if( stristr(PHP_OS, 'winnt') )
		{
			$cmd = 'cmd /c start "" "' . $file . '"';
		}
		elseif( stristr(PHP_OS, 'win32') )
		{
			$cmd = 'command /c start "" "' . $file . '"';
		}
		elseif( self::$os == 'macos' )
		{
			$cmd = 'open "' . $file . '"';
		}
		else
		{
			if( isset($_ENV['DESKTOP_LAUNCH']) ) {
				$cmd = $_ENV['DESKTOP_LAUNCH'] . '"' . $file . '"';
			}
			if ((isset($_ENV['KDE_FULL_SESSION']) && $_ENV['KDE_FULL_SESSION'] == 'true') ||
			(isset($_ENV['KDE_MULTIHEAD']) && $_ENV['KDE_MULTIHEAD'] == 'true'))
			{
				$cmd = 'kfmclient exec "' . $file . '"';
			}
			elseif (isset($_ENV['GNOME_DESKTOP_SESSION_ID']) || isset($_ENV['GNOME_KEYRING_SOCKET']))
			{
				$cmd = 'gnome-open "' . $file . '"';
			}
			else
			{
				$cmd = $file;
			}
		}

		return self::exec($cmd);
	}


  /**
   * Метод для создания скриншота рабочего стола
   *
   *
   * @param string $filename
   * @param string $ext
   */
  public static function screenshot($filename, $ext)
  {
    while(Gtk::events_pending())
    {
      Gtk::main_iteration();
    }

    $root = Gdk::get_default_root_window();
    list($width, $height) = $root->get_size();

    $pixbuf = new GdkPixbuf(Gdk::COLORSPACE_RGB, TRUE, 8, $width, $height);
    $pixbuf->get_from_drawable($root, $root->get_colormap(), 0, 0, 0, 0, $width, $height);
    $pixbuf->save($filename, $ext);
  }
  
}
?>